package com.gensdai.leliang.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gensdai.leliang.R;

/**
 * Created by Administrator on 2017/6/9.
 */

public class MyCartcardView extends LinearLayout {

    TextView subtract;
    TextView add;
    TextView numberText;

    private int number = 0;

    MyCartcardView.CustomAddOrSubtraceCallBack callBack;

    public void setCustomAddOrSubtraceCallBack(MyCartcardView.CustomAddOrSubtraceCallBack callback) {
        this.callBack = callback;
    }

    public MyCartcardView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initCustom(context, attrs);
    }

    public MyCartcardView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initCustom(context, attrs);
    }


    //初始化组件
    private void initCustom(Context context, AttributeSet attrs) {
        View view = inflate(context, R.layout.custom_add_delete, this);
        subtract = (TextView) view.findViewById(R.id.subtractBtn);
        add = (TextView) view.findViewById(R.id.addBtn);
        numberText = (TextView) view.findViewById(R.id.numberText);

        number = 1;
        numberText.setText("1");

        subtract.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (number > 1) {
                    number = number - 1;
                    numberText.setText(number + "");

                    if (callBack != null) {
                        callBack.subtrace(number);
                    }
                }
            }
        });
        add.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                number = number + 1;
                numberText.setText(number + "");
                if (callBack != null) {
                    callBack.add(number);
                }
            }
        });

    }

    public void setCentreText(String number){
        this.number = Integer.parseInt(number);
        numberText.setText(number);
    }


    /*定义回调接口 add or delete or touchNumber*/
    public interface CustomAddOrSubtraceCallBack {
        //增加按钮的回调
        void add(int number);

        //减少按钮的回调
        void subtrace(int number);

        //点击中间文本的回调
        void touchNumber();
    }
}
